/**
 * Created by DukeCrushIt on 2015/8/10.
 */
module dukeutil{
    export class FanGroup extends egret.Shape{
        public unitDrawAngle:number = 0.12;
        public radius:number;
        public constructor(radius:number){
            super();
            this.radius = radius;
            this.touchEnabled = false;
        }
        /**
         * [{label:"",percent:0-1,color:?},....]//total must be 100
         * */
        public init(data:any[]){
            var len:number = data.length;
            var percent:number;
            var color:number;
            var startAngle:number = 0;
            var endAngle:number = 0;
            var circle:number = Math.PI * 2;
            var item:Object;
            var lineangel:number = -1;
            while(len > 0){
                item = data[len-1];
                endAngle = startAngle + item["percent"]*circle;
                this.drawFan(startAngle,endAngle,item["color"]  !== undefined ? item["color"] : Math.random()*0xffffff);
                if( item["line"]){
                    lineangel = (startAngle + endAngle) * 0.5;
                }
                startAngle = endAngle;
                len--;
            }

            var g:egret.Graphics = this.graphics;
            var hr = this.radius >> 1;
            g.lineStyle(2,0x612b30);
            g.moveTo(hr*Math.cos(lineangel),-hr*Math.sin(lineangel));
            g.lineTo(115,115);
            g.lineTo(130,115);
        }

        public drawFan(startAngle:number,endAngle:number,color:number){
            var g:egret.Graphics = this.graphics;
            g.lineStyle(1,color);
            var tx:number;
            var ty:number;
            g.beginFill(color);
            var times:number = Math.ceil((endAngle - startAngle)/this.unitDrawAngle);
            var tempAngle:number = startAngle;

            g.moveTo(0,0);
            tx = this.radius*Math.cos(startAngle);
            ty = -this.radius*Math.sin(startAngle);
            g.lineTo(tx,ty);

            while(times > 0){
                if(times!=1){
                    tx = this.radius*Math.cos(tempAngle + this.unitDrawAngle);
                    ty = -this.radius*Math.sin(tempAngle + this.unitDrawAngle);
                }else{
                    tx = this.radius*Math.cos(endAngle);
                    ty = -this.radius*Math.sin(endAngle);
                }
                g.lineTo(tx,ty);

                tempAngle += this.unitDrawAngle;
                times--;
            }
            g.lineTo(0, 0);
            g.endFill();
        }
    }
}